const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isAllCheck: false,
    goodsList: [],
    total: 0
  },

  deleteGoods: function (e) {
    wx.showModal({
      content: '确定删除商品吗?',
      success: res => {
        if (res.confirm) {
          this.data.total = 0
          app.post('Car/del', {
            carid: e.currentTarget.dataset.cid
          }).then(res => {
            wx.showToast({
              title: '删除成功'
            })
            this.data.goodsList.map((item, index) => {
              if (item.c_id == e.currentTarget.dataset.cid) {
                item.checked = false
                this.data.goodsList.splice(index, 1)
              }
            })
            this.data.goodsList.map(item => {
              if (item.checked) {
                this.data.total += item.totalMoney
              }
            })
            this.setData({
              goodsList: this.data.goodsList,
              total: (this.data.total / 100 * 100).toFixed(2)
            })
          })
        }
      }
    })
  },

  checkGoods: function (e) {
    // console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    const items = this.data.goodsList
    const values = e.detail.value
    let total = 0
    for (let i = 0, lenI = items.length; i < lenI; ++i) {
      items[i].checked = false
      for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
        if (items[i].c_id == values[j]) {
          items[i].checked = true
          total += Number(items[i].totalMoney)
          break
        }
      }
    }
    this.setData({
      items,
      total: (total / 100 * 100).toFixed(2),
      isAllCheck: values.length == this.data.goodsList.length,
    })
  },

  allCheck: function (e) {
    this.data.total = 0
    if (this.data.isAllCheck) {
      this.data.goodsList.map(item => {
        item.checked = false
      })
      this.setData({
        goodsList: this.data.goodsList,
        isAllCheck: false,
        total: 0
      })
    } else {
      this.data.goodsList.map(item => {
        item.checked = true
        this.data.total += item.totalMoney
      })
      this.setData({
        goodsList: this.data.goodsList,
        isAllCheck: true,
        total: (this.data.total / 100 * 100).toFixed(2)
      })
    }
  },

  toSubmit: function (e) {
    let list = this.data.goodsList
    let str = ''
    list.map(item => {
      if (item.checked) {
        item.g_label.map(items => {
          if (items.count != 0) {
            str += `${item.g_id}-${items.name}-${items.count}*`
          }
        })
      }
    })
    str = str.substring(0, str.length - 1)

    if (str && this.data.total) {
      wx.navigateTo({
        url: '/pages/address/address?goodsInfo=' + str + '&total=' + this.data.total,
      })
    } else {
      wx.showModal({
        content: '请填写商品数量或是选择商品',
        showCancel: false
      })
    }
  },


  goodsCount: function (e) {
    let index = e.currentTarget.dataset.index
    let name = e.currentTarget.dataset.name
    let arr = this.data.goodsList[index].g_label
    let totalMoney = 0
    let unitPrice = this.data.goodsList[index].g_price
    let goodsMoney = `this.data.goodsList[${index}]`
    arr.map(item => {
      if (item.name == name) {
        item.count = e.detail.value | 0
      }
      totalMoney += unitPrice * item.count
    })
    this.data.goodsList[index].totalMoney = totalMoney
    this.data.total = 0
    this.data.goodsList.map(item => {
      if (item.checked) {
        this.data.total += item.totalMoney
      }
    })
    this.setData({
      [goodsMoney]: this.data.goodsList[index],
      total: (this.data.total / 100 * 100).toFixed(2)
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.post('Car/index', {
      token: wx.getStorageSync('token')
    }).then(res => {
      let list = res.data.result_data || []
      list.map(item => {
        item.checked = false,
          item.totalMoney = 0
      })
      this.setData({
        goodsList: list
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})